import numpy as np
import matplotlib.pyplot as plt
# 阶跃函数
def step_function(x):
    '''当输入超过0时，输出1，否则输出0.
        params
        -------------
        x: 输入参数（numpy 数组）
        return 
        ------------
        与输入数组等长的numpy数组，元素为0 or 1
    '''
    y = x>0
    return y.astype(np.int32)

def sigmoid(x):
    return 1/(1+np.exp(-x))


def relu(x):
    return np.maximum(0,x) 

def identity_function(x):
    return x 

def softmax(x):
    if x.ndim == 2:
        x = x.T
        x = x-np.max(x,axis=0)
        y = np.exp(x)/np.sum(np.exp(x),axis=0)
        return y.T
    x=x-np.max(x)
    return np.exp(x)/np.sum(np.exp(x))

    

def init_network():
    network = {}
    network['W1'] = np.array([[0.1,0.3,0.5],[0.2,0.4,0.6]])
    network['b1'] = np.array([0.1,0.2,0.3])
    network['W2'] = np.array([[0.1,0.4],[0.2,0.5],[0.3,0.6]])
    network['b2'] = np.array([0.1,0.2])
    network['W3'] = np.array([[0.1,0.3],[0.2,0.4]])
    network['b3'] = np.array([0.1,0.2])
    return network

def forward(network,x):
    W1,W2,W3 = network['W1'],network['W2'],network['W3']
    b1,b2,b3 = network['b1'],network['b2'],network['b3']

    a1 = np.dot(x,W1) + b1
    z1 = sigmoid(a1)

    a2 = np.dot(z1,W2) + b2
    z2 = sigmoid(a2)

    a3 = np.dot(z2,W3) + b3
    y = identity_function(a3)
    return y

if __name__ == '__main__':
    # network = init_network()
    # x = np.array([1.0,0.5])
    # y = forward(network,x)
    # print(y) #[0.31682708 0.69627909]
    x = np.array([[0.3,2.9,4.0],[1,2,3]])
    ret = softmax(x)
    print(ret)
    